//
//  UIView+Extension.swift

import UIKit
enum GradientType {
    case left2right,top2bottom
}
extension UIView {
    
    func addConstraintsWithFormat(format: String, views: UIView...) {
           var viewsDictionary = [String: UIView]()
           for (index, view) in views.enumerated() {
               let key = "v\(index)"
               viewsDictionary[key] = view
               view.translatesAutoresizingMaskIntoConstraints = false
           }
           
           addConstraints(NSLayoutConstraint.constraints(withVisualFormat: format, options: NSLayoutConstraint.FormatOptions(), metrics: nil, views: viewsDictionary))
       }
    
    
    /// X值
    public var ks_x: CGFloat {
        get {
            return self.frame.origin.x
        }
        set {
            var r = self.frame
            r.origin.x = newValue
            self.frame = r
        }

    }
    
    /// Y值
    public var ks_y: CGFloat {
        get {
            return self.frame.origin.y
        }
        set {
            var r = self.frame
            r.origin.y = newValue
            self.frame = r
        }

    }
    ///
    public var ks_middleY: CGFloat {
        get {
            return self.frame.size.height / 2 + self.ks_y
        }
        set {
            var r = self.frame
            r.origin.y = newValue - r.size.height / 2
            self.frame = r
        }

    }
    /// maxY值
    public var ks_maxY: CGFloat {
        get {
            return self.frame.maxY
        }
        set {
             var r = self.frame.size
            r.height = newValue - self.frame.origin.y
            self.frame.size = r
         }

    }
    /// maxX值
    public var ks_maxX: CGFloat {
        get {
            return self.frame.maxX
        }
        set {
             var r = self.frame.size
            r.width = newValue - self.frame.origin.x
            self.frame.size = r
         }

    }
    
    /// 中心点X
    public var ks_centerX: CGFloat {
        get {
            return self.center.x
        }
        set {
            var r = self.center
            r.x = newValue
            self.center = r
        }

    }
    
    /// 中心点Y
    public var ks_centerY: CGFloat {
        get {
            return self.center.y
        }
        set {
            var r = self.center
            r.y = newValue
            self.center = r
        }

    }
    
    /// 宽度
    public var ks_width: CGFloat {
        get {
            return self.frame.size.width
        }
        set {
            var r = self.frame.size
            r.width = newValue
            self.frame.size = r
        }

    }
    
    /// 高度
    public var ks_height: CGFloat {
        get {
            return self.frame.size.height
        }
        set {
            var r = self.frame.size
            r.height = newValue
            self.frame.size = r
        }
    }
    
    /// 尺寸
    public var ks_size: CGSize {
        get {
            return self.frame.size
        }
        set {
            var r = self.frame.size
            r = newValue
            self.frame.size = r
        }

    }

    
    /// 底部
    public var ks_bottom: CGFloat {
        get {
            let frame = self.frame
            return frame.maxY
        }
        set {
            let frame = self.frame
            let bottom = frame.maxY
            var r = bottom
            r = newValue
            self.frame.origin.y = r - self.frame.size.height
        }
        
    }
    
    /// 坐标
    public var ks_point: CGPoint {
        get {
            return self.frame.origin
        }
        set {
            var r = self.frame.origin
            r = newValue
            self.frame.origin = r
        }

    }
    
    public func viewFromXib() -> UIView {
        return Bundle.main.loadNibNamed(String(describing: self), owner: nil, options: nil)!.last! as! UIView
    }
    
    
    /**

    * 设置uiview 的任意圆角  单独圆角
     [UIRectCorner.bottomLeft,UIRectCorner.bottomRight,UIRectCorner.topLeft, UIRectCorner.topRight]
     传入要圆角的方位 和 圆角大小
     
    **/
    
    
   // FIXME: 这个方法对于 cell 的动态变化有bug，慎用，慎用
    func setMutiBorderRoundingCorners(corner:UIRectCorner,number:CGFloat)

    {

        let maskPath = UIBezierPath.init(roundedRect: self.bounds,

        byRoundingCorners: corner,

        cornerRadii: CGSize(width: number, height: number))

        let maskLayer = CAShapeLayer()

        maskLayer.frame = self.bounds

        maskLayer.path = maskPath.cgPath

        self.layer.mask = maskLayer

    }

 
    
}
// 圆角效果View
class CornerView:UIView {
   private override init(frame: CGRect) {
        super.init(frame: frame)
     }
    var innerView = UIView()
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    // 造一个圆角 正方形  round  outColor：圆弧外的颜色 innerColor：圆弧内的颜色
    convenience init (width:CGFloat,outColor:UIColor,innerColor:UIColor,type:ViewRoundType) {
        self.init(frame: CGRect(x: 0, y: 0, width: width, height: width))
        self.backgroundColor = outColor
        self.layer.masksToBounds = true
        
        
        if type == .leftTop {
            innerView.frame = CGRect(x: 0, y: 0, width: width * 2, height: width * 2)
        }
        if type == .rightTop {
            innerView.frame = CGRect(x: -width, y: 0, width: width * 2, height: width * 2)
        }
        if type == .leftBottom {
            innerView.frame = CGRect(x: 0, y: -width, width: width * 2, height: width * 2)
        }
        if type == .rightBottom {
            innerView.frame = CGRect(x: -width, y: -width, width: width * 2, height: width * 2)
        }
        innerView.backgroundColor = innerColor
        innerView.layer.masksToBounds = true
        innerView.layer.cornerRadius = width
        self.addSubview(innerView)
     }
    // 重置颜色
    func resetColor(outColor:UIColor,innerColor:UIColor) {
        self.backgroundColor = outColor
        innerView.backgroundColor = innerColor
    }
}



enum ViewRoundType {
    case leftTop,leftBottom,rightTop,rightBottom
}
/**

* 设置uiview 的任意边框

**/


func SetBorderWithView(_ view:UIView,top:Bool,left:Bool,bottom:Bool,right:Bool,width:CGFloat,color:UIColor)

{
    
    if top

    {

        let layer = CALayer()

        layer.frame = CGRect(x: 0, y: 0, width: view.frame.size.width, height: width)

        layer.backgroundColor = color.cgColor

        view.layer.addSublayer(layer)

    }

    if left

    {

        let layer = CALayer()

        layer.frame = CGRect(x: 0, y: 0, width: width, height: view.frame.size.height)

        layer.backgroundColor = color.cgColor

        view.layer.addSublayer(layer)

    }

    if bottom

    {

        let layer = CALayer()

        layer.frame = CGRect(x: 0, y: view.frame.size.height - width, width: width, height: width)

        layer.backgroundColor = color.cgColor

        view.layer.addSublayer(layer)

    }

    if right

    {

        let layer = CALayer()

        layer.frame = CGRect(x: view.frame.size.width - width, y: 0, width: width, height: view.frame.size.height)

        layer.backgroundColor = color.cgColor

        view.layer.addSublayer(layer)

    }

}
class MyCanvas: UIView {
  
     
    override func draw(_ rect: CGRect) {
        let pathRect = self.bounds.insetBy(dx: 1, dy: 1)
        let path = UIBezierPath(roundedRect: pathRect, cornerRadius: 22)
        path.lineWidth = 1
        UIColor.white.setFill()
        #colorLiteral(red: 0.8039215803, green: 0.8039215803, blue: 0.8039215803, alpha: 1).setStroke()
        path.fill()
        path.stroke()
    }
}
extension UIView {
    @IBInspectable var cornerRadius: CGFloat {
        set {
            layer.cornerRadius = newValue
            layer.masksToBounds = newValue > 0
        }

        get {
            return layer.cornerRadius
        }
    }

    @IBInspectable var borderWidth: CGFloat {
        set {
            layer.borderWidth = newValue
        }
        get {
            return layer.borderWidth
        }
    }
    @IBInspectable var borderColor: UIColor? {
        set {
            layer.borderColor = newValue?.cgColor
        }
        get {
            return layer.borderColor != nil ? UIColor(cgColor: layer.borderColor!) : nil
        }
    }

}




private func pixel(_ num: Double) -> Double {
    var unit: Double
    switch Int(UIScreen.main.scale) {
    case 1: unit = 1.0 / 1.0
    case 2: unit = 1.0 / 2.0
    case 3: unit = 1.0 / 3.0
    default: unit = 0.0
    }
    return roundbyunit(num, &unit)
}

private func ceilbyunit(_ num: Double, _ unit: inout Double) -> Double {
    return num - modf(num, &unit) + unit
}

private func floorbyunit(_ num: Double, _ unit: inout Double) -> Double {
    return num - modf(num, &unit)
}

private func roundbyunit(_ num: Double, _ unit: inout Double) -> Double {
    let remain = modf(num, &unit)
    if remain > unit / 2.0 {
        return ceilbyunit(num, &unit)
    } else {
        return floorbyunit(num, &unit)
    }
}

extension UIView {
    
    func kt_addCorner(_ radius: CGFloat) {
        kt_addCorner(radius, borderWidth: 1, backgroundColor: UIColor.clear, borderColor: UIColor.black)
    }
    
    func kt_addCorner(_ radius: CGFloat,
                      borderWidth: CGFloat,
                      backgroundColor: UIColor,
                      borderColor: UIColor) {
        
        let imageView = UIImageView(image: kt_drawRectWithRoundedCorner(radius, borderWidth: borderWidth, backgroundColor: backgroundColor, borderColor: borderColor))
        insertSubview(imageView, at: 0)
    }
    
    func kt_drawRectWithRoundedCorner(_ radius: CGFloat,
                                      borderWidth: CGFloat,
                                      backgroundColor: UIColor,
                                      borderColor: UIColor) -> UIImage {
        
        let sizetoFit = CGSize(width: pixel(Double(bounds.size.width)), height: Double(bounds.size.height))
        
        let halfBorderWidth = CGFloat(borderWidth / 2.0)
        
        UIGraphicsBeginImageContextWithOptions(sizetoFit, false, UIScreen.main.scale)
        let context = UIGraphicsGetCurrentContext()
        
        context?.setLineWidth(borderWidth)
        context?.setStrokeColor(borderColor.cgColor)
        context?.setFillColor(backgroundColor.cgColor)
        
        let width = sizetoFit.width
        let height = sizetoFit.height
        
        context?.move(to: CGPoint(x: width - halfBorderWidth, y: radius + halfBorderWidth))
        context?.addArc(tangent1End: CGPoint(x: width - halfBorderWidth, y: height - halfBorderWidth), tangent2End: CGPoint(x: width - radius - halfBorderWidth, y: height - halfBorderWidth), radius: radius)
        context?.addArc(tangent1End: CGPoint(x: halfBorderWidth, y: height - halfBorderWidth), tangent2End: CGPoint(x: halfBorderWidth, y: height - radius - halfBorderWidth), radius: radius)
        context?.addArc(tangent1End: CGPoint(x: halfBorderWidth, y: halfBorderWidth), tangent2End: CGPoint(x: width - halfBorderWidth, y: halfBorderWidth), radius: radius)
        context?.addArc(tangent1End: CGPoint(x: width - halfBorderWidth, y: halfBorderWidth), tangent2End: CGPoint(x: width - halfBorderWidth, y: radius + halfBorderWidth), radius: radius)
        
        UIGraphicsGetCurrentContext()?.drawPath(using: .fillStroke)
        let output = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return output!
    }
    
}

extension UIView {
    @objc func doneClicked(_ sender: UIBarButtonItem) {
        self.endEditing(true)
        self.resignFirstResponder()
    }
 
}

extension UIViewController {
    @objc func doneClicked(_ sender: UIBarButtonItem) {
        self.view.endEditing(true)
    }
}


